﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace LightLibrary {
	public static class IOTools {
		// === Public =================================================================================================
		public static List<FileInfo> GetAllFiles(DirectoryInfo directoryInfo) {
			return new List<FileInfo>(directoryInfo.GetFiles("*.*", SearchOption.AllDirectories));
		}

		public static List<FileInfo> GetFilesByExtensions(DirectoryInfo directoryInfo, string[] wantedExtensions) {
			var files = new List<FileInfo>();
			files.AddRange(directoryInfo.GetFiles("*.*", SearchOption.AllDirectories));
			return (from file in files let ext = file.Extension where wantedExtensions.Contains(ext) select file).ToList();
		}

		public static string GetDirectoryPath() {
			var dialog = new FolderBrowserDialog {
				Description = @"Open a folder for scanning",
				ShowNewFolderButton = false,
				SelectedPath = Application.StartupPath,
				RootFolder = Environment.SpecialFolder.MyComputer
			};

			return dialog.ShowDialog() == DialogResult.OK ? dialog.SelectedPath : null;
		}

		public static void CopyFile(string sourceFileName, string rootPathLibrary) {
			var file = new FileInfo(sourceFileName);
			file.CopyTo(Path.Combine(rootPathLibrary, file.Name), false);
		}

		public static string CopyFileAndGetPath(string sourceFileName, string rootPathLibrary) {
			var file = new FileInfo(sourceFileName);
			file.CopyTo(Path.Combine(rootPathLibrary, file.Name), false);
			return Path.Combine(rootPathLibrary, file.Name);
		}

		public static string CopyFileAndGetPath(FileInfo sourceFileName, string rootPathLibrary) {
			sourceFileName.CopyTo(Path.Combine(rootPathLibrary, sourceFileName.Name), false);
			return Path.Combine(rootPathLibrary, sourceFileName.Name);
		}
	}
}